﻿
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DBHelper;

namespace ZoneLine
{
    public class PlcListViewModel : ViewModelBase
    {
        private t_Plc _selectedModel;
        private List<t_Plc> _plcs;
        private RelayCommand _delCmd;

        public override bool Init()
        {
            this.Models = DbContext.GetModels<t_Plc>(  "1=1");
            return true;
        }

        public t_Plc SelectedModel { get => _selectedModel; set => Set(ref _selectedModel, value); }

        public List<t_Plc> Models { get => _plcs; private set => Set(ref _plcs, value); }

        public RelayCommand DelCmd => (_delCmd ?? (_delCmd = new RelayCommand(Del, CanDel)));

        private async void Del()
        {
            if (SelectedModel == null)
            {
                return;
            }
           
            if (await Confirm($"确定删除编号为【{SelectedModel.NO}】的信息?"))
            {
                bool rlt =DbContext.DeleteModel<t_Plc>($"NO = '{SelectedModel.NO}'");
                if (rlt)
                {
                    ShowMsg("删除成功！");
                }
                else
                {
                    ShowMsg("删除失败！");
                }
                Init();
            }
        }

        private bool CanDel()
        {

            return SelectedModel != null && SelectedModel.ID > 0;
        }
    }
}
